package com.kit.data.kitopeso.adapter;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewbinding.ViewBinding;


import com.kit.data.kitopeso.R;
import com.kit.data.kitopeso.base.BaseBindingAdapter1;
import com.kit.data.kitopeso.databinding.KitoFbOnlineItem1Binding;
import com.kit.data.kitopeso.databinding.KitoFbOnlineItem2Binding;
import com.kit.data.kitopeso.databinding.KitoFbOnlineItem3Binding;
import com.kit.data.kitopeso.obj.KitoChatInfo;
import com.kit.data.kitopeso.tools.ResUtils;
import com.kit.data.kitopeso.view.KitoSpaceItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class ChatAdapter extends BaseBindingAdapter1<KitoChatInfo, ViewBinding> {


    BtnItem3ClickListener onBtnItem3ClickListener;

    public ChatAdapter(List<KitoChatInfo> dataList) {
        super(dataList);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void bindData(ViewBinding binding, KitoChatInfo item, int position) {

        if (binding instanceof KitoFbOnlineItem1Binding) {
            KitoFbOnlineItem1Binding binding1 = (KitoFbOnlineItem1Binding) binding;
            binding1.tvTitle.setText(item.getTalkInfo());
            binding1.getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onBtnItemClickListener != null) {
                        onBtnItemClickListener.onItemClick(position);
                    }
                }
            });
        } else if (binding instanceof KitoFbOnlineItem2Binding) {
            KitoFbOnlineItem2Binding binding1 = (KitoFbOnlineItem2Binding) binding;
            binding1.tvTitle.setText(item.getTalkInfo());
            if (item.getState() == 0) {
                binding1.tvIcon2.setImageResource(R.mipmap.fb_online_state1);
            } else {
                binding1.tvIcon2.setImageResource(R.mipmap.fb_online_state2);
            }
            if (position == dataList.size() - 1) {
                binding1.tvHit.setVisibility(View.VISIBLE);
            } else {
                binding1.tvHit.setVisibility(View.GONE);
            }
            binding1.getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onBtnItemClickListener != null) {
                        onBtnItemClickListener.onItemClick(position);
                    }
                }
            });
        } else if (binding instanceof KitoFbOnlineItem3Binding) {
            KitoFbOnlineItem3Binding binding1 = (KitoFbOnlineItem3Binding) binding;
            List<String> itemList = new ArrayList<>();
            itemList.add(ResUtils.s(R.string.feeback_online_item_subtitle1));
            itemList.add(ResUtils.s(R.string.feeback_online_item_subtitle2));
            itemList.add(ResUtils.s(R.string.feeback_online_item_subtitle3));
            itemList.add(ResUtils.s(R.string.feeback_online_item_subtitle4));
            itemList.add(ResUtils.s(R.string.feeback_online_item_subtitle5));
            itemList.add(ResUtils.s(R.string.feeback_online_item_subtitle6));
            ChatItemAdapter chatItemAdater = new ChatItemAdapter(itemList);
            chatItemAdater.setOnBtnItemClickListener(position1 -> {
                if (onBtnItem3ClickListener != null) {
                    onBtnItem3ClickListener.onItemClick(itemList.get(position1), position1);
                }
            });
            LinearLayoutManager layoutManager1 = new LinearLayoutManager(binding1.recyclerView.getContext());
            binding1.recyclerView.setLayoutManager(layoutManager1);
            int d = ResUtils.d(R.dimen.dp_1);
            KitoSpaceItemDecoration decoration = new KitoSpaceItemDecoration(0, 0, binding1.recyclerView.getContext());
            decoration.setDividerHeight(d);
            decoration.setmDivider(ResUtils.de(R.drawable.fb_online_chat_line));
            binding1.recyclerView.addItemDecoration(decoration);
            binding1.recyclerView.setAdapter(chatItemAdater);
        }

    }

    @Override
    protected int getItemType(KitoChatInfo item) {
        if (TextUtils.isEmpty(item.getTalkType())) {
            return 3;
        } else if (item.getTalkType().equals("CS")) {
            return 1;
        }
        return 2;
    }

    @Override
    protected ViewBinding createBinding(LayoutInflater inflater, ViewGroup parent, int viewType) {
        if (viewType == 1) {
            return KitoFbOnlineItem1Binding.inflate(inflater, parent, false);
        } else if (viewType == 2) {
            return KitoFbOnlineItem2Binding.inflate(inflater, parent, false);
        }
        return KitoFbOnlineItem3Binding.inflate(inflater, parent, false);
    }


    public interface BtnItem3ClickListener {
        void onItemClick(String item, int position);
    }

    public void setOnBtnItem3ClickListener(BtnItem3ClickListener onBtnItem3ClickListener) {
        this.onBtnItem3ClickListener = onBtnItem3ClickListener;
    }
}
